using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Services.FileSystem;
using Deepcode.Flux.Core.Systems.Asset;
using Deepcode.Flux.Core.Extensions;

public partial class Private_Asset_Actions_DeleteFolder : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxAmAccess"; } }
	#endregion
	#region ViewState and Properties
	/// <summary>
	/// Store the path passed in if appropriate
	/// </summary>
	protected string Path
	{
		get
		{
			object o = ViewState["Path"];
			if( o == null ) return "";
			return (string) o;
		}
		set
		{
			ViewState["Path"] = value;
		}
	}
	#endregion
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Load path data
			Path = GetAttribute("Item", "");
			if( ! Path.StartsWith("~/assets/")) GoBack( false, false );

			try
			{
				string fullpath = Server.MapPath(Path);
				System.IO.Directory.Delete(fullpath, true);

				Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseImportant(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
					"Flux", "Asset Manager",
					"deleted folder {0}", fullpath);

				
				Path = Path.Substring( 0, Path.LastIndexOf('/'));
			}
			catch { }
			GoBack(true, true);
		}
	}
	/// <summary>
	/// Go back to the previous form
	/// </summary>
	/// <param name="refreshFolders"></param>
	/// <param name="refreshFiles"></param>
	protected void GoBack(bool refreshFolders, bool refreshFiles)
	{
		string url = "~/admin/private/asset/actions.aspx?path=" + this.Path;
		if( refreshFolders ) url += "&refreshFolders=true";
		if( refreshFiles ) url += "&refreshFiles=true";

		Response.Redirect(url, true);
	}
}
